import json
import os
from algorithm import *
from flask import Flask, request, jsonify
from flask_cors import CORS
import shutil
app = Flask(__name__)
CORS(app)


@app.route("/Logon/register",methods = ['POST'])
def Register():
        username = request.form.get("username")
        password = request.form.get("password")
        flags = register(username,password)
        print(username,password)
        if flags == "1":
            return jsonify({"message":"用户名已存在，请勿重复注册"})
        elif flags == "2":
            return jsonify({"message":"格式错误，请检查你的密码长度"})
        elif flags == "3":
            return jsonify({"message":"格式错误，请检查你的密码中是否同时包含数字和字母"})
        else:
            return jsonify({"message":"注册成功"})


# 登录逻辑
@app.route('/Login/login', methods=['POST','GET'])
def Login():
    if request.method=='GET':
        username=request.args.get('username')
        password=request.args.get('password')
    else:
        username = request.form.get("username")
        password = request.form.get("password")
    print(username,password)

    flag=login(username,password)
    if flag=="1":
        return jsonify({"message": "用户名不存在"})#, 401
    elif flag=="2":
        return  jsonify({"message": "密码错误"})#, 401
    else:
        return jsonify({"message":"登录成功","username": username, "password": password})

if __name__ == '__main__':
    app.run()